package com.xuecheng.content.controller;

import com.xuecheng.api.content.CourseBaseApi;
import com.xuecheng.api.content.model.dto.CourseBaseDTO;
import com.xuecheng.api.content.model.qo.QueryCourseModel;
import com.xuecheng.api.content.model.vo.CourseAuditVo;
import com.xuecheng.api.content.model.vo.CourseBaseVo;
import com.xuecheng.common.domain.page.PageRequestParams;
import com.xuecheng.common.domain.page.PageVO;
import com.xuecheng.common.util.SecurityUtil;
import com.xuecheng.content.convert.CourseBaseConvert;
import org.springframework.web.bind.annotation.*;
import com.xuecheng.content.service.CourseBaseService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.ModelAndView;

import java.util.Map;

/**
 * <p>
 * 课程基本信息 前端控制器
 * </p>
 *
 * @author itcast
 */
@Slf4j
@RestController
public class CourseBaseController implements CourseBaseApi {

    @Autowired
    private CourseBaseService  courseBaseService;


    //查询课程信息列表
    @PostMapping("/course/list")
    public PageVO queryCourseList(PageRequestParams params, @RequestBody QueryCourseModel model){

        //获取令牌，解析得到公司id
        Long companyId = SecurityUtil.getCompanyId();

        return courseBaseService.QueryCourseList(params, model,companyId);
    }


    //保存课程信息内容
    @PostMapping("/course")
    public CourseBaseDTO creatCourseBase(@RequestBody CourseBaseVo courseBaseVo) {

        //获取令牌
        Long companyId = SecurityUtil.getCompanyId();

        //将CourseBaseVo转为CourseBaseDto
       CourseBaseDTO courseBaseDto = CourseBaseConvert.INSTANCE.vo2dto(courseBaseVo);

       //将令牌赋值给CourseBaseDto
        courseBaseDto.setCompanyId(companyId);

        //调用service

        return courseBaseService.creatCourseBase(courseBaseDto);
    }

    @GetMapping("/course/{courseBaseId}")
    public CourseBaseDTO queryCourseById(@PathVariable Long courseBaseId) {
        //1.获得公司Id值
        Long companyId = SecurityUtil.getCompanyId();

        //2.获得课程基本信息数据

        return courseBaseService.queryById(courseBaseId,companyId);
    }

    @PutMapping("/course")
    public CourseBaseDTO modifyCourse(@RequestBody CourseBaseVo courseBaseVo) {

        //1.将VO数据转为DTO数据
        CourseBaseDTO courseBaseDTO = CourseBaseConvert.INSTANCE.vo2dto(courseBaseVo);

        //2.通过工具类获得公司和用户信息
        Long companyId = SecurityUtil.getCompanyId();
        courseBaseDTO.setCompanyId(companyId);

        return courseBaseService.modifyCourse(courseBaseDTO);
    }

    @DeleteMapping("/course/{courseBaseId}")
    public void deleteCourseBase(@PathVariable Long courseBaseId) {

        //获取令牌，解析得到公司id
        Long companyId = SecurityUtil.getCompanyId();

        courseBaseService.deleteCourseBase(courseBaseId,companyId);
    }

    @GetMapping("/course/commit/{courseBaseId}")
    public void submitCourseBase(@PathVariable Long courseBaseId) {

        //获取令牌
        Long companyId = SecurityUtil.getCompanyId();

        courseBaseService.submitCourseBase(courseBaseId,companyId);
    }

    @PostMapping("/m/course/list")
    public PageVO runQueryCourseList(PageRequestParams params, @RequestBody QueryCourseModel model) {

        //运营平台查询不需要教育机构id，所以为null

        return courseBaseService.QueryCourseList(params, model, null);
    }

    @PostMapping("m/courseReview/approve")
    public void modifyCourseAudit(@RequestBody CourseAuditVo courseAuditVo) {

        CourseBaseDTO courseBaseDTO = CourseBaseConvert.INSTANCE.audit2to(courseAuditVo);

        courseBaseService.modifyCourseAudit(courseBaseDTO);
    }


    @GetMapping("/course/preview/{courseBaseId}/{companyId}")
    public Object CoursePreview(@PathVariable Long courseBaseId, @PathVariable Long companyId) {
        //获得数据模型数据
        Map<String, Object> dataMap = courseBaseService.CoursePreview(courseBaseId, companyId);
        //获得页面模型
        return new ModelAndView("learing_article",dataMap);
    }


}
